package com.sinosoft.cloud.access.abc.service;

import com.alibaba.tamper.BeanMapping;
import com.sinosoft.cloud.access.annotations.AccessService;
import com.sinosoft.cloud.access.service.AccessServiceBean;
import com.sinosoft.cloud.access.util.TimeUtil;
import com.sinosoft.cloud.nb.app.client.ContInputService;
import com.sinosoft.cloud.nb.app.client.MSPremCalService;
import com.sinosoft.cloud.rest.TradeInfo;
import com.sinosoft.lis.entity.GlobalPojo;
import com.sinosoft.lis.entity.LCAppntPojo;
import com.sinosoft.lis.entity.LCContPojo;
import com.sinosoft.lis.entity.LCInsuredPojo;
import feign.RetryableException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by Administrator on 2017-9-15.
 * 新单试算
 */
@AccessService(name = "abc", transCode = "1022")
public class PremReckonService extends AccessServiceBean {

    /**
     * 日志管理
     */
    private final Log cLogger = LogFactory.getLog(this.getClass());

    @Autowired
    private ContInputService contInputService;

    @Autowired
    private MSPremCalService msPremCalService;


    @Override
    public TradeInfo service(TradeInfo reqTradeInfo) {

        //错误信息
        String errorStr = "";
        String errorLog = "";
        long microServiceStart = 0L;
        long microServiceEnd = 0L;

        //若为01渠道，则将投保人五要素赋给被保人五要素
        GlobalPojo globalPojo = (GlobalPojo) reqTradeInfo.getData(GlobalPojo.class.getName());
        String entrustWay = globalPojo.getEntrustWay();
        if ("ABC_EBank".equals(entrustWay)) {
            LCAppntPojo lcAppntPojo = (LCAppntPojo) reqTradeInfo.getData(LCAppntPojo.class.getName());
            List<LCInsuredPojo> insuredList = (List<LCInsuredPojo>) reqTradeInfo.getData(LCInsuredPojo.class.getName());
            LCInsuredPojo lcInsuredPojo = insuredList.get(0);
            BeanMapping.create(LCAppntPojo.class, LCInsuredPojo.class).mapping(lcAppntPojo, lcInsuredPojo);
            insuredList.clear();
            insuredList.add(lcInsuredPojo);
            reqTradeInfo.addData(LCInsuredPojo.class.getName(), insuredList);
        }

        /**
         * 调用微服务接口i
         */
        TradeInfo respTradeInfo = null;
        LCContPojo lcp = new LCContPojo();


        //生成固定保单号
        lcp = (LCContPojo) reqTradeInfo.getData(LCContPojo.class.getName());
        lcp.setContNo("1111111111");
        reqTradeInfo.removeData(LCContPojo.class.getName());
        reqTradeInfo.addData(LCContPojo.class.getName(), lcp);

        //调用保单结构微服务
        try {
            cLogger.info("1.开始调用保单结构微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = contInputService.service(reqTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "保单结构微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "保单结构", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "保单结构", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用保单结构微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("保单结构微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }


        //调用保费计算微服务
        try {
            cLogger.info("2.开始调用保费计算微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = msPremCalService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "保费计算微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "保费计算", e);
            return getErrorTradeInfo(errorStr);
        } catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "保费计算", e);
            return getErrorTradeInfo(errorStr);
        }
        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用保费计算微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("保费计算微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        return respTradeInfo;
    }

}